package cn.bocweb.encryption;


import java.security.Key;
import java.security.Security;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;


public class AES256Encryption {

    public static final String KEY_ALGORITHM = "AES";

    public static final String CIPHER_ALGORITHM = "AES";

    public static byte[] initkey() throws Exception {

        //实例化密钥生成器
        Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
        KeyGenerator kg = KeyGenerator.getInstance(KEY_ALGORITHM, "BocJkq");
        kg.init(256);
        kg.init(128);

        SecretKey secretKey = kg.generateKey();

        return secretKey.getEncoded();
    }

    public static byte[] initRootKey() throws Exception {

        return new byte[]{0x08, 0x08, 0x04, 0x0b, 0x02, 0x0f, 0x0b, 0x0c,
                0x01, 0x03, 0x09, 0x07, 0x0c, 0x03, 0x07, 0x0a, 0x04, 0x0f,
                0x06, 0x0f, 0x0e, 0x09, 0x05, 0x01, 0x0a, 0x0a, 0x01, 0x09,
                0x06, 0x07, 0x09, 0x0d};

    }

    public static Key toKey(byte[] key) throws Exception {

        SecretKey secretKey = new SecretKeySpec(key, KEY_ALGORITHM);

        return secretKey;
    }

    public static byte[] encrypt(byte[] data, byte[] key) throws Exception {

        Key k = toKey(key);

        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);

        cipher.init(Cipher.ENCRYPT_MODE, k);

        return cipher.doFinal(data);
    }


    public static byte[] hexStringToBytes(String hexString) {
        if (hexString == null || hexString.equals("")) {
            return null;
        }
        hexString = hexString.toUpperCase();
        int length = hexString.length() / 2;
        char[] hexChars = hexString.toCharArray();
        byte[] d = new byte[length];
        for (int i = 0; i < length; i++) {
            int pos = i * 2;
            d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));

        }
        return d;
    }

    private static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }

    public static String decryptObject(String str, byte[] key) {
        String decryptString = "";
        String decryptStrings = "";
        try {
            byte[] data = hexStringToBytes("37045f45c28cdb0a4a88a182ffa3370ce4708772f66b1d1cc665e171e6718af1");
            String aaa = new String(data, "utf-8");

            Key k = toKey(key);
            //  Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
            Cipher cipher = Cipher.getInstance("aes/ecb/pkcs5padding");

            cipher.init(Cipher.ENCRYPT_MODE, k);

            decryptString = new String(cipher.doFinal(data), "utf-8");

            decryptStrings = new String(decryptString.getBytes("iso-8859-1"), "utf-8");

        } catch (Exception e) {
        }
        return decryptStrings;
    }

    public static byte[] decrypt(byte[] data, byte[] key) throws Exception {

        Key k = toKey(key);

        // Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
        //Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
        Cipher cipher = Cipher.getInstance("AES/ECB/ZeroBytePadding");
        cipher.init(Cipher.DECRYPT_MODE, k);

        return cipher.doFinal(data);
    }

    private static byte[] decrypt(byte[] src, String key) throws Exception {

        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);

        SecretKeySpec securekey = new SecretKeySpec(key.getBytes(), CIPHER_ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, securekey);
        return cipher.doFinal(src);
    }

    public  static String decrypt(String data,String CRYPT_KEY) {
        try {
            return new String(decrypt(hex2byte(data.getBytes()), CRYPT_KEY));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static byte[] hex2byte(byte[] b) {

        byte[] b2 = new byte[b.length / 2];
        for (int n = 0; n < b.length; n += 2) {
            String item = new String(b, n, 2);
            b2[n / 2] = (byte) Integer.parseInt(item, 16);
        }
       return b2;

    }
}

